<?php

namespace App\Miya\Models;

use Encore\Admin\Auth\Database\Menu;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Route;

class Module extends Model
{
    protected $guarded = [];

    protected static function booted()
    {
        // 触发删除时同步删除分类和信息的导航链接
        static::deleted(function ($model) {
            $id = $model->id;

            $classifyMenu = Menu::where('uri', 'module/' . $id . '/classifies')->first();
            $infoMenu = Menu::where('uri', 'module/' . $id . '/infos')->first();

            $classifyMenu->delete();
            $infoMenu->delete();
        });
    }

    protected $casts = [
        'module_extends' => 'json',
        'classify_extends' => 'json',
        'info_extends' => 'json',
    ];

    static public $oneInfoTypes = [
        1 => '开启',
        2 => '关闭'
    ];

    public function getModuleExtendsAttribute($value)
    {
        return array_values(json_decode($value, true) ?: []);
    }

    public function setModuleExtendsAttribute($value)
    {
        $this->attributes['module_extends'] = json_encode(array_values($value), JSON_UNESCAPED_UNICODE);
    }

    public function getClassifyExtendsAttribute($value)
    {
        return array_values(json_decode($value, true) ?: []);
    }

    public function setClassifyExtendsAttribute($value)
    {
        $this->attributes['classify_extends'] = json_encode(array_values($value), JSON_UNESCAPED_UNICODE);
    }

    public function getInfoExtendsAttribute($value)
    {
        return array_values(json_decode($value, true) ?: []);
    }

    public function setInfoExtendsAttribute($value)
    {
        $this->attributes['info_extends'] = json_encode(array_values($value), JSON_UNESCAPED_UNICODE);
    }

    public function keyword()
    {
        return $this->belongsTo(Keyword::class, 'keyword_id');
    }

    public function classifies()
    {
        return $this->hasMany(Classify::class);
    }

    public function infos()
    {
        return $this->hasMany(Info::class);
    }

    public function types()
    {
        return $this->hasMany(ModuleType::class, 'module_id');
    }

    /**
     * 生成模块展示页面 或者模块分类页面url
     * @param bool $is_classify 是否生成分类页面
     * @return string
     */
    public function url($is_classify = true)
    {
        $current = Route::currentRouteName();
        $prefix = explode('.', $current)[0];
        $data = [$this->tag];
        $routeName = null;
        switch ($prefix) {
            case 'pc':
                $routeName = $is_classify ? 'pc.classify' : 'pc.module';
                break;
            case 'area':
                $area = request()->route('area');
                $data = Arr::collapse([[$area->tag], $data]);
                $routeName = $is_classify ? 'area.classify' : 'area.module';
                break;
            case 'm':
                $routeName = $is_classify ? 'm.classify' : 'm.module';
                break;
            case 'domain_area':
                $area = request()->route('area');
                $data = Arr::collapse([[$area->tag], $data]);
                $routeName = $is_classify ? 'domain_area.classify' : 'domain_area.module';
                break;
        }

        return route($routeName, $data);
    }
}
